<template>
    <div class="select-shop-warp" :class="{'have-bor':!isIndex}">
        <Transition mode="out-in" name="slide-fade">
            <div class="warp-info" v-if="!createShopFlag">
                <div class="title-name">
                    <div class="bark" @click="back()"> <i  class="iconfont-admin icon-zuojiantou-copy"></i>返回</div>
                </div>
                <div class="title-name">
                    <div class="left-title"> 您可以进入以下管理后台 </div>
                    <div class="right-item">
                        <el-button v-if="isIndex" @click="createShopFlag=true" type="primary">创建店铺管理后台</el-button>
                    </div>
                </div>
                <div class="sub-title-name" v-if="userinfo !== '' && userinfo?.username">
                    <span>{{ userinfo?.username }}</span>
                    已绑定了以下店铺，您可以进入以下任一店铺
                </div>
                <a-spin :spinning="loading" style="width:100%;margin-top:100px;height: 200px" />
                <div v-if="!loading && userinfo?.admin_type === 'admin'" class="shop-list shop-list-min-height">
                    <div  @click="toIndex(0)"  class="shop-item" style="border-color: #ffeac7;background-color: #fff4e6;color: #4E4E4E;">
                        <div class="left-item">
                            <img class="left-image" src="@/assets/logo/shop_logo_b.png" alt="">
                            <div class="left-info">
                                <div class="left-title">{{ userinfo?.username }}(管理后台)</div>
                            </div>
                        </div>
                        <div class="right-item">
                            <span class="iconfont-admin icon-login-youjiantou"></span>
                        </div>
                    </div>
                </div>
                <div v-if="myShopList.length>0" class="shop-list shop-list-min-height">
                    <div v-if="!loading" @click="toIndex(item.shop_id)" v-for="item in myShopList" class="shop-item">
                        <div class="left-item">
                            <el-image class="left-image" :src="shopLogo"></el-image>
                            <div class="left-info">
                                <div class="left-title">{{ item.shop_title }}</div>
                            </div>
                        </div>
                        <div class="right-item">
                            <span class="iconfont-admin icon-login-youjiantou"></span>
                        </div>
                    </div>

                </div>
                <div v-if="!loading&&myShopList.length==0&&userinfo?.admin_type != 'admin'" class="shop-list shop-list-min-height">
                    <el-empty description="您还没有已绑定的店铺哦～" />
                </div>
            </div>
            <div v-else>
                <el-form
                    label-width="70"
                    ref="formRef"
                    :model="formState"
                    class="demo-ruleForm"
                    status-icon
                >
                    <el-form-item :rules="[{ required: true, message: '店铺名称不能为空!' }]" label="店铺名称" prop="shop_title">
                        <el-input v-model="formState.shop_title" />
                    </el-form-item>
                    <el-form-item label="店铺logo" prop="shop_logo">
                        <FormAddGallery v-model:photo="formState.shop_logo"></FormAddGallery>
                    </el-form-item>
                    <el-form-item label="">
                        <el-button type="primary" @click="onSubmit()">
                            创建
                        </el-button>
                        <el-button @click="createShopFlag=false">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </Transition>
    </div>
</template>
<script setup lang="ts">
import shopLogo from "@/assets/logo/shop_logo.png";
import { imageFormat } from "@/utils/format";
import { onMounted, ref, shallowRef ,defineEmits} from "vue";
import { createShop, getShopMyShop } from "@/api/merchant/shop";
import { useUserStore } from "@/store/user";
import { useConfigStore } from "@/store/config";
import { useMenusStore } from "@/store/menu";
import { message, notification } from "ant-design-vue";
import { useRouter } from "vue-router";
import { updateMenu } from "@/utils/menus";
import type { MainMenu } from "@/types/common/common.d";
import { FormAddGallery } from "@/components/gallery";
import dayjs from "dayjs";
const props = defineProps({
    isIndex: {
        type: Boolean,
        default: false
    },
    selectShopFlag:{
        type: Boolean,
        default: false
    }

});
const formState = ref({
    shop_title: "",
    shop_logo: ""
});
const formLoading = ref(false);
// 表单通过验证后提交
const onSubmit = async () => {
    await formRef.value.validate();
    try {
        formLoading.value = true;
        const result = await createShop({ ...formState.value });
        message.success(result.message);
        createShopFlag.value = false;
        await _getShopMyShop()
    } catch (error: any) {
        message.error(error.message);
    } finally {
        formLoading.value = false;
    }
};
const loading = ref(true);
const formRef = shallowRef();
const createShopFlag = ref(false);
const emit = defineEmits(["closePopup", "update:selectShopFlag"]);
const router = useRouter();
const toIndex = async (shop_id: number) => {
    localStorage.setItem("shopId", String(shop_id));
    if(shop_id === 0){
        localStorage.setItem("adminType", 'admin');
    }else{
        localStorage.setItem("adminType", 'shop');
    }
    const userStore = useUserStore() as any;
    const configStore = useConfigStore();
    const menusStore = useMenusStore();
    // 更新后台设置项
    userStore.updateUserInfo();
    await configStore.updateConfig();
    notification["success"]({
        message: "登录成功",
        placement: "top",
        duration: 1.5,
        description: "您好，欢迎回来"
    });
    if (props.isIndex) {
        //关闭弹窗
        emit("closePopup");
    }
    // 切换到商铺选择页面
    let mainMenu = await updateMenu() as MainMenu[];
    menusStore.mainMenu = mainMenu;
    router.push("/" + (mainMenu ? mainMenu[0].route_link : ""));
    localStorage.setItem("lastOpenTime", String(dayjs().unix()));
};
const { logout } = useUserStore();
const back = () => {
    emit('update:selectShopFlag', false);
    logout();
}
const myShopList = ref([]);
const userinfo = ref("")
const _getShopMyShop = async () => {
    try {
        loading.value = true;
        const result = await getShopMyShop();
        myShopList.value = result.filter_result;
        userinfo.value = result.userinfo;
    } catch (error: any) {
        message.error(error.message);
    } finally {
        loading.value = false;
    }
};
onMounted(() => {
    const accessToken = localStorage.getItem("accessToken");
    if (accessToken) {
        _getShopMyShop();
    }
});
</script>
<style scoped lang="less">

.select-shop-warp {

    background-color: #FFFFFF;
    padding: 20px 30px;
    box-sizing: border-box;

    .warp-info {
        display: flex;
        flex-direction: column;
        gap: 16px;

        .title-name {
            display: flex;
            justify-content: space-between;
            .bark{
                display: flex;
                align-items: center;
                font-size: 14px;
                font-weight: 600;
                gap: 2px;
                cursor: pointer;
            }

            .left-title {
                font-size: 22px;
                font-weight: bold;
            }

            .right-item {
                font-size: 14px;

            }
        }

        .sub-title-name {
            font-size: 14px;
            line-height: 1.5;

            & > span {
                font-weight: bold;
            }
        }

        .shop-list {
            display: flex;
            flex-direction: column;
            gap: 8px;
            .shop-item {
                cursor: pointer;
                border: .6px solid #e0e0e0;
                padding: 10px;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: space-between;
                border-radius: 10px;


                .left-item {
                    flex: 1;
                    display: flex;
                    flex-direction: row;
                    align-items: center;
                    gap: 14px;

                    .left-image {
                        width: 60px;
                        height: 60px;
                        border-radius: 10px;
                    }

                    .left-info {
                        display: flex;
                        flex-direction: column;
                        gap: 14px;

                        .left-title {
                            font-size: 16px;
                            font-weight: bold;
                        }

                        .left-name {
                            display: flex;
                            align-items: center;
                            font-size: 14px;
                            color: #5b5b5b;
                            font-weight: 500;
                        }
                    }
                }

                .right-item {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }
            }
        }
        .shop-list-min-height{
            min-height: 200px;

        }

        .admin-list{
            display: flex;
            flex-direction: column;
            gap: 8px;
            .shop-item {
                cursor: pointer;
                border: .6px solid #e0e0e0;
                padding: 10px;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: space-between;
                border-radius: 10px;


                .left-item {
                    flex: 1;
                    display: flex;
                    flex-direction: row;
                    align-items: center;
                    gap: 14px;

                    .left-image {
                        width: 60px;
                        height: 60px;
                        border-radius: 10px;
                    }

                    .left-info {
                        display: flex;
                        flex-direction: column;
                        gap: 14px;

                        .left-title {
                            font-size: 16px;
                            font-weight: bold;
                        }

                        .left-name {
                            display: flex;
                            align-items: center;
                            font-size: 14px;
                            color: #5b5b5b;
                            font-weight: 500;
                        }
                    }
                }

                .right-item {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }
            }
        }
    }
}

/*
  进入和离开动画可以使用不同
  持续时间和速度曲线。
*/
.slide-fade-enter-active {
    transition: all 0.3s ease-out;
}

.slide-fade-leave-active {
    transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-fade-enter-from,
.slide-fade-leave-to {
    transform: translateX(20px);
    opacity: 0;
}

</style>

